package com.wang.learn.cloudfeture.demo;

import com.wang.learn.cloudfeture.pojo.LockDemo;
import com.wang.learn.cloudfeture.pojo.SynchronizedDemo;
import com.wang.learn.cloudfeture.pojo.VolatileDemo;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ClassName ThreadDemo
 *
 * @author wang
 * Date 2022/4/1 9:55
 */
public class ConcurrentTest {

    public static int num  = 0;

    public  ExecutorService executorService  = Executors.newFixedThreadPool(10);

    public  CountDownLatch countDownLatch = new CountDownLatch(1);


    @Test
    public void synchronizedDemo() throws Exception {
        for (int i = 0; i < 30; i++) {
            SynchronizedDemo runnableDemo = new SynchronizedDemo(countDownLatch);
            executorService.execute(runnableDemo);
        }
        countDownLatch.countDown();
        System.out.println("线程集结完毕");
        Thread.sleep(2000);
        System.out.println("================================");
        System.out.println(num);
    }

    @Test
    public void volatileDemo() throws Exception {
        for (int i = 0; i < 100; i++) {
            VolatileDemo runnableDemo = new VolatileDemo(countDownLatch);
            executorService.execute(runnableDemo);
        }
        countDownLatch.countDown();
        System.out.println("线程集结完毕");
        Thread.sleep(2000);
        System.out.println("================================");
        System.out.println(num);
    }

    @Test
    public void lockDemo() throws Exception {
        for (int i = 0; i < 10; i++) {
            LockDemo runnableDemo = new LockDemo();
            runnableDemo.setCountDownLatch(countDownLatch);
            executorService.execute(runnableDemo);
        }
        countDownLatch.countDown();
        System.out.println("线程集结完毕");
        Thread.sleep(2000);
        System.out.println("================================");
        System.out.println(num);
    }




}
